import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;

import CustomSwing.JCLabel;
import CustomSwing.TableColumnAdjuster;
import CustomSwing.TableColumnHider;

@SuppressWarnings("serial")
public class ListOfVendorsPanel extends JPanel {
	public String USER_FUNC = "List of Vendors";
	private Font custFont = new Font("Arial Bold", Font.BOLD, 15);

	Db db;
	private String[] columnNames = {"Vendor ID", "First Name", "Last Name", "Address", 
			"Phone", "Email", "UBI", "Membership Paid", "Business Name", "Stall #"};
	private JCheckBox[] checkBoxes = new JCheckBox[columnNames.length];
	private String[][] data;
	private JScrollPane tableSP;
	private JPanel tableSPPanel = new JPanel(new BorderLayout());

	public ListOfVendorsPanel(Db theDB) {
		db = theDB;
		Border b = BorderFactory.createCompoundBorder(
				BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK), 
				BorderFactory.createEmptyBorder(3, 3, 3, 3));
		
		JPanel checkBoxesPanel = new JPanel(new GridLayout(2, 5));
		for (int i = 0; i < checkBoxes.length; i++) {
			checkBoxes[i] = new JCheckBox(columnNames[i]);
			setCheckBox(checkBoxes[i], i);
			checkBoxesPanel.add(checkBoxes[i]);
		}
		
		JPanel checkBoxOptionPanel = new JPanel(new BorderLayout());
		checkBoxOptionPanel.setBorder(b);
		checkBoxOptionPanel.add(new JCLabel("Filter:"), BorderLayout.NORTH);
		checkBoxOptionPanel.add(checkBoxesPanel);
		
		setLayout(new BorderLayout(5, 5));
		add(checkBoxOptionPanel, BorderLayout.NORTH);
		add(tableSPPanel, BorderLayout.CENTER);
		
		refreshTablePanel();
	}
	
	public void refreshTablePanel() {
		tableSPPanel.removeAll();
		getData();
		tableSPPanel.add(tableSP);
		tableSPPanel.revalidate();
	}

	private void getData() {
		try {
			ArrayList<String> vIDs = db.getVIDs();
			String[] vendorData;
			data = new String[vIDs.size()][columnNames.length];
			for (int i = 0; i < vIDs.size(); i++) {
				vendorData = db.viewVendor(vIDs.get(i));
				for (int j = 0; j < vendorData.length; j++) {
					if (vendorData[j] == null)
						data[i][j] = "";
					else
						data[i][j] = " "+vendorData[j]+" ";
				}
			}
			setTable();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void setTable() {
		JTable table = new JTable(data, columnNames);
		table.setFont(new Font(custFont.getName(), Font.PLAIN, custFont.getSize()-2));
		table.setEnabled(false);
		table.setRowHeight(20);
		table.getTableHeader().setFont(custFont);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumnAdjuster tca = new TableColumnAdjuster(table);
		tca.adjustColumns();
		tableSP = new JScrollPane(table);
		tableSP.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK), 
				BorderFactory.createEmptyBorder(3, 3, 3, 3)));
		hideShowColumns(table);
	}
	
	private void hideShowColumns(JTable table) {
		for (int i = 0; i < columnNames.length; i++) {
			if (checkBoxes[i].isSelected())
				new TableColumnHider(table).show(columnNames[i]);
			else
				new TableColumnHider(table).hide(columnNames[i]);
		}
	}
	
	private void setCheckBox(JCheckBox cb, int i) {
		cb.setSelected(true);
		cb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				refreshTablePanel();
			}
		});
	}
}
